package com.example.warehouse.sys.service.impl;


import com.example.warehouse.common.ex.ServiceException;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.web.ServiceCode;
import com.example.warehouse.sys.dao.persist.respository.IDeptRepository;
import com.example.warehouse.sys.pojo.entity.SysDept;
import com.example.warehouse.sys.pojo.entity.SysUser;
import com.example.warehouse.sys.pojo.param.AddNewDeptParam;
import com.example.warehouse.sys.pojo.param.DeptUpdateInfoParam;
import com.example.warehouse.sys.pojo.vo.DeptListItemVO;
import com.example.warehouse.sys.pojo.vo.DeptStandardVO;
import com.example.warehouse.sys.service.IDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class DeptServiceImpl implements IDeptService {

    @Value("${warehouse.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    private IDeptRepository iDeptRepository;

    @Override
    public void addNew(AddNewDeptParam addNewDeptParam) {
        String name = addNewDeptParam.getName();
        int count = iDeptRepository.countByName(name);
        if (count > 0) {
            String message = "新增部门失败，部门名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        SysDept dept = new SysDept();
        BeanUtils.copyProperties(addNewDeptParam, dept);
        int rows = iDeptRepository.insert(dept);
        if (rows != 1) {
            String message = "新增标签类别失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        DeptStandardVO current = iDeptRepository.getStandardById(id);
        if (current== null || current.getId() == 0) {
            String message = "删除部门失败，尝试删除的部门数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        int rows = iDeptRepository.deleteById(id);
        if (rows != 1) {
            String message = "删除部门失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }

    }

    @Override
    public void updateInfoById(Long id,DeptUpdateInfoParam deptUpdateInfoParam) {
        DeptStandardVO current=iDeptRepository.getStandardById(id);
        if (current == null) {
            String message = "修改标签失败，尝试修改的部门数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        String name=deptUpdateInfoParam.getName();
        int count=iDeptRepository.countByNameAndNotId(id,name);
        if (count > 0) {
            String message = "修改部门失败，部门名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        SysDept dept=new SysDept();
        BeanUtils.copyProperties(deptUpdateInfoParam,dept);
        System.out.println("你好"+dept);
        int rows=iDeptRepository.updateById(dept);
        if (rows != 1) {
            String message = "修改部门失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public PageData<DeptListItemVO> list(Integer pageNum) {
        PageData<DeptListItemVO> pageData = iDeptRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<DeptListItemVO> list(Integer pageNum, Integer pageSize) {
        PageData<DeptListItemVO> pageData = iDeptRepository.list(pageNum, pageSize);
        return pageData;
    }

    @Override
    public PageData<DeptListItemVO> listByParentId(Long id, Integer pageNum) {
        PageData<DeptListItemVO> pageData = iDeptRepository.listByParentId(id, pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<DeptListItemVO> listByParentId(Long id, Integer pageNum, Integer pageSize) {
        PageData<DeptListItemVO> pageData = iDeptRepository.listByParentId(id, pageNum, pageSize);
        return pageData;
    }

    @Override
    public List<SysDept> selectAll() {
        List<SysDept> list = iDeptRepository.selectAll();
        return list;
    }

    @Override
    public DeptStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询类别详情】的业务，参数：{}", id);
        DeptStandardVO queryResult = iDeptRepository.getStandardById(id);
        if (queryResult == null) {
            String message = "查询类别详情失败，类别数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }


}